# -*- coding: utf-8 -*-
# @Time       : 2019/8/18 下午7:01
# @Author     : Simon
# @Email      :
# @File       : Test.py
# @Software   : PyCharm
# @Description:
import threading

import os

from db import Session, ShopMeituanInfo, MTSpider, ShopEleInfo, ELESpider, updYseOrderInfo
import datetime

if __name__=="__main__":
    session = Session()
    mtshops = session.query(ShopMeituanInfo).filter(ShopMeituanInfo.deleted == 0).all()
    eleshops = session.query(ShopEleInfo).filter(ShopEleInfo.deleted == 0).all()
    session.close()

    type = 1
    ##type = 1,为每一个美团店铺生成详细订单，type = 2，为每个饿了么店铺生成详细订单
    orderDayNum = 4
    ##index等于1表示，获取昨天一天的订单，index = 4表示，获取今天之前4天到昨天的数据
    mins = 30
    ##每个mins分钟累计一次,每个30分钟存储到yes_order_info表格的InfoList字段，每隔10分钟的存储到yes_order_info表格的orderDetail
    orderDayNum = -1 * orderDayNum
    if type == 1:
        for shop in mtshops:
            try:
                if not shop.account == '':
                    cookie=shop.cookies
                    mt=MTSpider(cookie)
                    daysNum = mt.get7daysOrder(orderDayNum)
                    allOrderLists = []
                    ct = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:00")
                    for dayNum in daysNum:
                        if len(dayNum) != 0:
                            dayNum.sort()
                            timelst = []
                            for order in dayNum:
                                timelst.append(order.split(' ')[1])
                            dayperiod = {}
                            i = 0
                            j = 15
                            index = 0
                            a = 0
                            while i < 24:
                                while j <= 60:
                                    if j == 60:
                                        j = 0
                                        break
                                    else:
                                        t = "%02d:%02d:00" % (i, j)
                                        dayperiod[t] = 0
                                        while index < len(timelst) and timelst[index] < t:
                                            dayperiod[t] = dayperiod[t] + 1
                                            index += 1
                                        j += 15
                                i += 1
                            dayperiod["23:59:59"] = 0
                            for tl in timelst:
                                if tl > '23:45:00' and tl <= '23:59:59':
                                    a += 1
                            dayperiod["23:59:59"] = a
                            orderList = []  # 目前orderlist是96个
                            for key, value in dayperiod.items():
                                orderList.append(value)
                            periodOrderList = []
                            index = 0
                            if mins == 30:
                                # 将俩个俩个相加，变成48个
                                while index <= 94:
                                    periodOrderList.append(orderList[index] + orderList[index + 1])
                                    index += 2
                            elif mins == 60:
                                while index <= 92:
                                    periodOrderList.append(
                                        orderList[index] + orderList[index + 1] + orderList[index + 2] + orderList[index + 3])
                                    index += 4
                            elif mins == 15:
                                periodOrderList = orderList[:]
                            periodOrderList = str(periodOrderList)
                            allOrderLists.append(periodOrderList)
                    for i in range(orderDayNum, 0):
                        daytime = datetime.datetime.now() + datetime.timedelta(days=i)
                        daytime = daytime.strftime("%Y-%m-%d")
                        updYseOrderInfo(shop.account, allOrderLists[i - orderDayNum], daytime, ct)
            except:
                print(shop.mtName +  "获取订单失败")
    else :
        for shop in eleshops:
            try:
                cookie = shop.cookies
                ele = ELESpider(cookie)
                for i in range(orderDayNum, 0):
                    day = (datetime.datetime.now() + datetime.timedelta(days=i)).strftime("%Y-%m-%d")
                    daystart = day + "T00:00:00"
                    dayend = day + "T23:59:59"
                    daysNum = ele.getAllOrder(daystart, dayend, 200)

                    timelst = []
                    # 所有下单时间存进列表
                    if daysNum:
                        for dayNum in daysNum:
                            # 2019-10-01T21:22:09,返回的时间是这样的，只取21:22:09
                            timelst.append(dayNum.get("activeTime").split('T')[1])
                        timelst.sort()
                        dayperiod = {}
                        i = 0
                        j = 15
                        index = 0
                        a = 0
                        while i < 24:
                            while j <= 60:
                                if j == 60:
                                    j = 0
                                    break
                                else:
                                    t = "%02d:%02d:00" % (i, j)
                                    dayperiod[t] = 0
                                    while index < len(timelst) and timelst[index] < t:
                                        dayperiod[t] = dayperiod[t] + 1
                                        index += 1
                                    j += 15
                            i += 1
                        dayperiod["23:59:59"] = 0
                        for tl in timelst:
                            if tl > '23:45:00' and tl <= '23:59:59':
                                a += 1
                        dayperiod["23:59:59"] = a
                        orderList = []  # 目前orderlist是96个
                        for key, value in dayperiod.items():
                            orderList.append(value)

                        periodOrderList = []
                        index = 0
                        if mins == 30:
                            # 将俩个俩个相加，变成48个
                            while index <= 94:
                                periodOrderList.append(orderList[index] + orderList[index + 1])
                                index += 2
                        elif mins == 60:
                            while index <= 92:
                                periodOrderList.append(
                                    orderList[index] + orderList[index + 1] + orderList[index + 2] + orderList[index + 3])
                                index += 4
                        elif mins == 15:
                            periodOrderList = orderList[:]

                        periodOrderList = str(periodOrderList)
                        ct = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:00")
                        updYseOrderInfo(shop.account, periodOrderList, day, ct)
            except:
                print(shop.eleName + "获取订单失败")

